# Setup Versions
configure_file (${CMAKE_CURRENT_SOURCE_DIR}/Version.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/Version.h)


# Source files for toadlet_egg
set (EGG_SRC
	Assert.cpp
	Atomic.cpp
	Error.cpp
	Log.cpp
	MathFormatting.cpp
	Object.cpp
	Profile.cpp
	Random.cpp
	Socket.cpp
	String.cpp
	ThreadPool.cpp
	TUUID.cpp
	Version.cpp
)

set (EGG_IO_SRC
	io/DataStream.cpp
	io/FileStream.cpp
	io/MemoryStream.cpp
	io/PacketMessageStream.cpp
	io/SocketStream.cpp
)

set (EGG_MATH_SRC
	math/Capsule.cpp
	math/Math.cpp
	math/Matrix3x3.cpp
	math/Matrix4x4.cpp
	math/Plane.cpp
	math/Quaternion.cpp
	math/Sphere.cpp
)

set (EGG_MATHFIXED_SRC
	mathfixed/Capsule_fixed.cpp
	mathfixed/Math_fixed.cpp
	mathfixed/Matrix3x3_fixed.cpp
	mathfixed/Matrix4x4_fixed.cpp
	mathfixed/Plane_fixed.cpp
	mathfixed/Quaternion_fixed.cpp
	mathfixed/Sphere_fixed.cpp
)

set (EGG_PLATFORM_POSIX_SRC
	platform/posix/PosixDynamicLibrary.cpp
	platform/posix/PosixMutex.cpp
	platform/posix/PosixSystem.cpp
	platform/posix/PosixThread.cpp
	platform/posix/PosixWaitCondition.cpp
)

set (EGG_PLATFORM_ANDROID_SRC
	platform/android/AndroidAssetArchive.cpp
	platform/android/JStream.cpp
)

set (EGG_PLATFORM_OSX_SRC
	platform/osx/OSXBundleArchive.mm
	logit/PosixErrorHandler_osx.mm
)

set (EGG_PLATFORM_WIN32_SRC
	platform/win32/StreamIStream.h
	platform/win32/Win32DynamicLibrary.cpp
	platform/win32/Win32Mutex.cpp
	platform/win32/Win32ResourceArchive.cpp
	platform/win32/Win32System.cpp
	platform/win32/Win32Thread.cpp
	platform/win32/Win32WaitCondition.cpp
)


# Headers
set (EGG_HDR
	Assert.h
	Atomic.h
	BaseResource.h
	Categories.h
	CircularBuffer.h
	Collection.h
	DynamicLibrary.h
	EndianConversion.h
	Error.h
	Event.h
	Extents.h
	ExplicitIntrusivePointer.h
	Interface.h
	IntrusivePointer.h
	IteratorRange.h
	Log.h
	MathConversion.h
	MathFormatting.h
	Map.h
	Mutex.h
	Object.h
	PointerCounter.h
	Profile.h
	Random.h
	Resource.h
	ResourceDestroyedListener.h
	ResourceRequest.h
	Runnable.h
	Runner.h
	SharedPointer.h
	Socket.h
	String.h
	System.h
	Thread.h
	ThreadPool.h
	Type.h
	WaitCondition.h
	WeakPointer.h
	TUUID.h
	Version.h
)

set (EGG_IO_HDR
	io/Archive.h
	io/BaseArchive.h
	io/DataStream.h
	io/DataMessageStream.h
	io/FileStream.h
	io/MemoryStream.h
	io/MessageStream.h
	io/PacketMessageStream.h
	io/SocketStream.h
	io/Stream.h
	io/StreamRequest.h
)

set (EGG_MATH_HDR
	math/AABox.h
	math/BaseMath.h
	math/Capsule.h
	math/EulerAngle.h
	math/Math.h
	math/Matrix3x3.h
	math/Matrix4x4.h
	math/Plane.h
	math/Quaternion.h
	math/Segment.h
	math/Sphere.h
	math/Vector2.h
	math/Vector3.h
	math/Vector4.h
)

set (EGG_MATHFIXED_HDR
	mathfixed/AABox.h
	mathfixed/BaseMath.h
	mathfixed/Capsule.h
	mathfixed/EulerAngle.h
	mathfixed/Math.h
	mathfixed/Matrix3x3.h
	mathfixed/Matrix4x4.h
	mathfixed/Plane.h
	mathfixed/Quaternion.h
	mathfixed/Segment.h
	mathfixed/Sphere.h
	mathfixed/Vector2.h
	mathfixed/Vector3.h
	mathfixed/Vector4.h
)

set (EGG_IO_STL_HDR
	io/Stream_iostream.h
	io/iostream_Stream.h
)

set (EGG_PLATFORM_POSIX_HDR
	platform/posix/PosixDynamicLibrary.h
	platform/posix/PosixMutex.h
	platform/posix/PosixSystem.h
	platform/posix/PosixThread.h
	platform/posix/PosixWaitCondition.h
)

set (EGG_PLATFORM_ANDROID_HDR
	platform/android/AndroidAssetArchive.h
	platform/android/JStream.h
)

set (EGG_PLATFORM_OSX_HDR
	platform/osx/OSXBundleArchive.h
)

set (EGG_PLATFORM_WIN32_HDR
	platform/win32/Win32DynamicLibrary.h
	platform/win32/Win32Mutex.h
	platform/win32/Win32ResourceArchive.h
	platform/win32/Win32System.h
	platform/win32/Win32Thread.h
	platform/win32/Win32WaitCondition.h
)

if (TOADLET_EXCEPTIONS)
	set (EGG_IO_HDR ${EGG_IO_HDR} ${EGG_IO_STL_HDR})
endif (TOADLET_EXCEPTIONS)


# Bundle source
source_group ("Source Files\\\\io" FILES ${EGG_IO_SRC})


# Bundle headers
source_group ("Header Files\\\\io" FILES ${EGG_IO_HDR})



# Sources, headers and namespaces
set (EGG_SRC ${EGG_SRC} ${EGG_IO_SRC})
set (EGG_HDR ${EGG_HDR} ${EGG_IO_HDR})
set (EGG_NAMESPACES ${EGG_NAMESPACES} io)


# Always build both fixed & floating point math
# But include neither in the autogenerated header files (eg. not present in EGG_NAMESPACES)
set (EGG_SRC ${EGG_SRC} ${EGG_MATHFIXED_SRC})
set (EGG_HDR ${EGG_HDR} ${EGG_MATHFIXED_HDR})
source_group ("Source Files\\\\mathfixed" FILES ${EGG_MATHFIXED_SRC})
source_group ("Header Files\\\\mathfixed" FILES ${EGG_MATHFIXED_HDR})
set (EGG_SRC ${EGG_SRC} ${EGG_MATH_SRC})
set (EGG_HDR ${EGG_HDR} ${EGG_MATH_HDR})
source_group ("Source Files\\\\math" FILES ${EGG_MATH_SRC})
source_group ("Header Files\\\\math" FILES ${EGG_MATH_HDR})


# Platform specific files
if (TOADLET_PLATFORM_WIN32)	
	source_group ("Source Files\\\\platform\\\\win32" FILES ${EGG_PLATFORM_WIN32_SRC})
	source_group ("Header Files\\\\platform\\\\win32" FILES ${EGG_PLATFORM_WIN32_HDR})
	set (EGG_SRC ${EGG_SRC} ${EGG_PLATFORM_WIN32_SRC})
	set (EGG_HDR ${EGG_HDR} ${EGG_PLATFORM_WIN32_HDR})
elseif (TOADLET_PLATFORM_POSIX) 
	source_group ("Source Files\\\\platform\\\\posix" FILES ${EGG_PLATFORM_POSIX_SRC})
	source_group ("Header Files\\\\platform\\\\posix" FILES ${EGG_PLATFORM_POSIX_HDR})
	set (EGG_SRC ${EGG_SRC} ${EGG_PLATFORM_POSIX_SRC})
	set (EGG_HDR ${EGG_HDR} ${EGG_PLATFORM_POSIX_HDR})

	if (TOADLET_PLATFORM_OSX)
		source_group ("Source Files\\\\platform\\\\osx" FILES ${EGG_PLATFORM_OSX_SRC})
		source_group ("Header Files\\\\platform\\\\osx" FILES ${EGG_PLATFORM_OSX_HDR})
		set (EGG_SRC ${EGG_SRC} ${EGG_PLATFORM_OSX_SRC})
		set (EGG_HDR ${EGG_HDR} ${EGG_PLATFORM_OSX_HDR})
		find_library (FOUNDATION_LIBS Foundation)
		set (EGG_LIBS objc ${EGG_LIBS} ${FOUNDATION_LIBS})
	endif (TOADLET_PLATFORM_OSX)

	if (TOADLET_PLATFORM_ANDROID)
		source_group ("Source Files\\\\platform\\\\android" FILES ${EGG_PLATFORM_ANDROID_SRC})
		source_group ("Header Files\\\\platform\\\\android" FILES ${EGG_PLATFORM_ANDROID_HDR})
		set (EGG_SRC ${EGG_SRC} ${EGG_PLATFORM_ANDROID_SRC})
		set (EGG_HDR ${EGG_HDR} ${EGG_PLATFORM_ANDROID_HDR})
		set (EGG_LIBS ${EGG_LIBS} log)
	endif (TOADLET_PLATFORM_ANDROID)
	
	if ((NOT TOADLET_PLATFORM_ANDROID) AND (NOT TOADLET_PLATFORM_EMSCRIPTEN))
		set (EGG_LIBS ${EGG_LIBS} dl pthread)
	endif ((NOT TOADLET_PLATFORM_ANDROID) AND (NOT TOADLET_PLATFORM_EMSCRIPTEN))
endif (TOADLET_PLATFORM_WIN32)


# Give the headers and namespaces parent scope for inclusion into the master egg.h file
set (EGG_HDR ${EGG_HDR} PARENT_SCOPE)
set (EGG_NAMESPACES ${EGG_NAMESPACES} PARENT_SCOPE)


# If we are using swig create the wrapper file from the interface files
if (SWIG_FOUND AND EGG_SWIG_INT)
	swig_wrap_files (egg_wrap.cpp egg ${EGG_SWIG_INT} "${EGG_SWIG_INT_DEPS}" "${EGG_SWIG_INT_OUTPUTS}")	
	set (EGG_SRC ${EGG_SRC} egg_wrap.cpp)
endif (SWIG_FOUND AND EGG_SWIG_INT)


# For individual component installation 
set (EGG_INSTALL)
if (TOADLET_PLATFORM_ANDROID)
	set (EGG_INSTALL jtoadlet_egg)
endif (TOADLET_PLATFORM_ANDROID)


# Dynamic library
if (TOADLET_BUILD_DYNAMIC)
	add_library (toadlet_egg SHARED ${EGG_SRC} ${EGG_HDR})
	set_target_properties (toadlet_egg PROPERTIES ${DYNAMIC_LIB_PROP})
	target_compile_definitions (toadlet_egg PUBLIC ${DYNAMIC_LIB_DEFS})
	target_compile_options (toadlet_egg PUBLIC ${DYNAMIC_LIB_FLAG})
	target_link_libraries (toadlet_egg ${DYNAMIC_LIB_LINK} ${EGG_LIBS})
	install (TARGETS toadlet_egg DESTINATION ${LIB_INSTALL_DIR} COMPONENT egg)
	set (EGG_INSTALL ${EGG_INSTALL} toadlet_egg)
endif (TOADLET_BUILD_DYNAMIC)


# Static library
if (TOADLET_BUILD_STATIC)
	add_library (toadlet_egg_s STATIC ${EGG_SRC} ${EGG_HDR})
	set_target_properties (toadlet_egg_s PROPERTIES ${STATIC_LIB_PROP})
	target_compile_definitions (toadlet_egg_s PUBLIC ${STATIC_LIB_DEFS})
	target_compile_options (toadlet_egg_s PUBLIC ${STATIC_LIB_FLAG})
	target_link_libraries (toadlet_egg_s ${STATIC_LIB_LINK} ${EGG_LIBS})
	install (TARGETS toadlet_egg_s DESTINATION ${LIB_INSTALL_DIR} COMPONENT egg)
	set (EGG_INSTALL ${EGG_INSTALL} toadlet_egg_s)

	# HACK: install the library on iOS. This will disappear once cmake issue:0012506 is resolved. 
	if (TOADLET_PLATFORM_IOS)
	    install (DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/\$ENV{CONFIGURATION}\$ENV{EFFECTIVE_PLATFORM_NAME}/ DESTINATION ${LIB_INSTALL_DIR} COMPONENT egg FILES_MATCHING PATTERN "*.a")
	endif (TOADLET_PLATFORM_IOS)
endif (TOADLET_BUILD_STATIC)

# Rule to install only egg
add_custom_target (egg_install
	COMMAND ${CMAKE_COMMAND} -DBUILD_TYPE=$<CONFIGURATION> -DCOMPONENT=required -P ${PROJECT_BINARY_DIR}/cmake_install.cmake
	COMMAND ${CMAKE_COMMAND} -DBUILD_TYPE=$<CONFIGURATION> -DCOMPONENT=egg -P ${PROJECT_BINARY_DIR}/cmake_install.cmake
	DEPENDS ${EGG_INSTALL}
)


